package com.xr.zk.zul.listbox;

import lombok.Getter;
import lombok.Setter;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.event.PagingEvent;

import com.xr.common.bean.PageInfo;
import com.xr.test.ui.PagingCallback;

public class PagingListboxExt<T> extends Div implements IdSpace {
	private static final long serialVersionUID = 1L;
	@Setter
	private PagingCallback<T> pagingCallback;
	private final String zul = "/zul/common/listbox/paging_listbox.zul";
	@Getter
	private Listbox listbox;
	@Getter
	private Paging paging;
	private Div div;
	private int PAGE_SIZE;

	public PagingListboxExt() {
		Executions.createComponents(this.zul, this, null);
		ConventionWires.wireVariables(this, this);
		ConventionWires.addForwards(this, this);

		this.paging.addEventListener("onPaging",
				new EventListener<PagingEvent>() {
					public void onEvent(PagingEvent pagingEvent)
							throws Exception {
						PageInfo<T> pageInfo = pagingCallback.queryPageInfo(
								pagingEvent.getActivePage(), PAGE_SIZE);

						listbox.setModel(new ListModelList<T>(pageInfo
								.getDatas()));

						paging.setTotalSize(pageInfo.getTotalSize());
					}
				});
	}

	public void onCreate() {
		PAGE_SIZE = paging.getPageSize();

		for (org.zkoss.zk.ui.Component comp : this.getChildren()) {
			if (comp instanceof Listbox) {
				listbox = (Listbox) comp;
				listbox.setParent(div);
				return;
			}
		}
		
		
	}

}
